package com.zjdiepu.www.manage.api.third;

import io.swagger.annotations.ApiOperation;

import java.net.SocketTimeoutException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.lang.HttpClientException;
import com.zjdiepu.www.ibiz.third.IJuheBrandService;
import com.zjdiepu.www.ibiz.third.IJuheCarService;
import com.zjdiepu.www.ibiz.third.IJuheCityService;
import com.zjdiepu.www.ibiz.third.IJuheProvinceService;
import com.zjdiepu.www.ibiz.third.IJuheSeriesService;
import com.zjdiepu.www.ibiz.third.IJuheYearService;
import com.zjdiepu.www.ibiz.third.net.IUsedCarExactEstimationService;
import com.zjdiepu.www.model.third.dto.JuheBrand;
import com.zjdiepu.www.model.third.dto.JuheCar;
import com.zjdiepu.www.model.third.dto.JuheCity;
import com.zjdiepu.www.model.third.dto.JuheProvince;
import com.zjdiepu.www.model.third.dto.JuheSeries;
import com.zjdiepu.www.model.third.dto.JuheYear;
import com.zjdiepu.www.model.third.ext.JuheBrandExt;
import com.zjdiepu.www.model.third.ext.JuheCarExt;
import com.zjdiepu.www.model.third.ext.JuheCityExt;
import com.zjdiepu.www.model.third.ext.JuheProvinceExt;
import com.zjdiepu.www.model.third.ext.JuheSeriesExt;
import com.zjdiepu.www.model.third.ext.JuheYearExt;
import com.zjdiepu.www.model.third.request.juhe.JuheExactEstimationRequest;
import com.zjdiepu.www.model.third.response.ExactEstimationResponse;
import com.zjdiepu.www.model.third.response.juhe.JuheExactEstimationResponse;
/**
 * 项目名称：dp-rest <br/> 
 * 类名称：ExactEstimationController <br/>  
 * 类描述：二手车精确估价 <br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年3月27日 下午12:50:15  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年3月27日 下午12:50:15   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@RestController
@RequestMapping(value = "ExactEstimationController", produces = "application/json;charset=UTF-8")
public class ExactEstimationController extends BaseController {
	@Resource
	protected IJuheProvinceService juheProvinceService;
	@Resource
	protected IJuheCityService juheCityService;
	@Resource
	protected IJuheBrandService juheBrandService;
	@Resource
	protected IJuheSeriesService juheSeriesService;
	@Resource
	protected IJuheCarService juheCarService;
	@Resource
	protected IJuheYearService juheYearService;
	@Resource(name="juheUsedCarExactEstimationServiceImpl")
	protected IUsedCarExactEstimationService<JuheExactEstimationResponse, JuheExactEstimationRequest> juheUsedCarExactEstimationService;
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: queryProvince  <br/>
	 * @Description: 聚合数据：二手车接口：省份<br/>
	 * @param juheProvinceExt
	 * @return
	 * @version V1.0  <br/>
	 */
	@ApiOperation(value = "条件所有省",response = JuheProvince.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value="/province",method = RequestMethod.POST)
	public Map<String,Object> queryProvince(@ModelAttribute JuheProvinceExt juheProvinceExt)	{
		Map<String,Object> map=super.success();
		juheProvinceExt.withOutPage();
		juheProvinceExt.setBaseWhere();
		List<JuheProvince> provinces = juheProvinceService.selectByExt(juheProvinceExt);
		JuheCityExt juheCityExt= new JuheCityExt();
		juheCityExt.withOutPage();
		List<JuheCity> cities = juheCityService.selectByExt(juheCityExt);
		map.put("province", provinces);
		map.put("city", cities);
		return map;
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: queryProvince  <br/>
	 * @Description: 聚合数据：二手车接口：城市<br/>
	 * @param juheCityExt
	 * @return
	 * @version V1.0  <br/>
	 */
	@ApiOperation(value = "条件所有市",response = JuheCity.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value="/city",method = RequestMethod.POST)
	public ApiResponse queryCity(@ModelAttribute JuheCityExt juheCityExt)	{
		juheCityExt.withOutPage();
		juheCityExt.setBaseWhere();
		List<JuheCity> cities = juheCityService.selectByExt(juheCityExt);
		return ApiResponse.success(cities);
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: queryProvince  <br/>
	 * @Description: 聚合数据：二手车接口：品牌<br/>
	 * @param juheBrandExt
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "brand", method = { RequestMethod.GET })
	public ApiResponse queryBrand(JuheBrandExt juheBrandExt)	{
		juheBrandExt.withOutPage();
		juheBrandExt.setBaseWhere();
		List<JuheBrand> brands = juheBrandService.selectByExt(juheBrandExt);
		return ApiResponse.success(brands);
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: queryProvince  <br/>
	 * @Description: 聚合数据：二手车接口：系列<br/>
	 * @param juheSeriesExt
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "series", method = { RequestMethod.GET })
	public ApiResponse querySeries(JuheSeriesExt juheSeriesExt)	{
		juheSeriesExt.withOutPage();
		juheSeriesExt.setBaseWhere();
		List<JuheSeries> series = juheSeriesService.selectByExt(juheSeriesExt);
		return ApiResponse.success(series);
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: queryProvince  <br/>
	 * @Description: 聚合数据：二手车接口：车型<br/>
	 * @param juheCarExt
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "car", method = { RequestMethod.GET })
	public ApiResponse queryCar(JuheCarExt juheCarExt)	{
		juheCarExt.withOutPage();
		juheCarExt.setBaseWhere();
		List<JuheCar> cars = juheCarService.selectByExt(juheCarExt);
		return ApiResponse.success(cars);
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: queryProvince  <br/>
	 * @Description: 聚合数据：二手车接口：车型年份<br/>
	 * @param juheYearExt
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "year", method = { RequestMethod.GET })
	public ApiResponse queryYear(JuheYearExt juheYearExt)	{
		juheYearExt.withOutPage();
		juheYearExt.setBaseWhere();
		List<JuheYear> years = juheYearService.selectByExt(juheYearExt);
		return ApiResponse.success(years);
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: queryProvince  <br/>
	 * @Description: 聚合数据：二手车接口：估值<br/>
	 * @param juheYearExt
	 * @return
	 * @version V1.0  <br/>
	 * @throws HttpClientException 
	 * @throws SocketTimeoutException 
	 */
	@RequestMapping(value = "estimate", method = { RequestMethod.POST })
	public ApiResponse estimate(@Validated JuheExactEstimationRequest request) throws HttpClientException	{
		try {
			ExactEstimationResponse exactEstimation = juheUsedCarExactEstimationService.exactEstimation(request);
			return ApiResponse.success(exactEstimation);
		} catch (SocketTimeoutException e) {
			//try n times again .
			return ApiResponse.fail(ResultFlagType.FAIL);
		}
	}
}
